
Maths	=	function()
{
	
}

/*!	@brief This function will take 2 interval [a;b], [c;d], and a t value.
 *  The t value must be an existing one in the first interval [a;b]
 *
 * 	The goal of the function is to transform the t value to make it match to 
 *  the second interval.
 *
 * 	For instance, if we define our 2 interval as such : [0;100] and [0;50]
 *  If t = 50 in [0;100], it would be equal to 25 in [0;50]
 */
Maths.IntervalToInterval	=	function( a, b, c, d, t)
{
	var lengthIntervalAB =	Math.abs(a-b); 
	var percentage	=	t/lengthIntervalAB;

	return ((percentage)*d + ((1 - (percentage))*c));
}


/*!	@brief This function will check if the specified point named A is inside
 *	a polygon.
 *
 *	The Polygon is represented by a list of points, each of them must share
 * 	the same plane
 *
 *	To determine if a Point is inside a Polygon, we must, for each point P, 
 * compute the sum of  Pn A P(n+1) angles. If the sum of every angles is 
 *  equals to 2PI, then, the point is inside the polygone.
 */
 
 Maths.IsPointInPolygon	=	function(A,	P)
 {
	 var sumAngle=0.0;

	 for(var i=0;i < P.length; i++)
	 {
		 var APn	=	P[i].Minus(A);
		 var APnp1	=	P[ (i+1)%P.length].Minus(A);	//vector AP(n+1)
		
		APn.w		=0.0;
		APnp1.w		=0.0;
		
		 var	denominateur = APn.Length() * APnp1.Length();
		 var angle = Vec4.Dot(APn, APnp1);
		 angle = angle / denominateur;
		 angle = Math.acos(angle);
		 console.log("AngleValue :" + angle);
		 sumAngle+=angle;
	 }
	 console.log("Angle : "+ sumAngle);
	 return ( Maths.IsEqual(sumAngle, Math.PI*2, 0.1));
	 
 }
 
 /*! @brief Return true if A is between C and D : C < A < D */
 Maths.IsBetween	=	function(A, C, D)
 {
	 if((A>= C) && ( A <=D) )
	 {
		 return true;
	 }
	 return false;
 }
 
 /*! @brief Return true if A equal B. As float can't be perfectly equals, we 
  *  must use a delta value, to "estimate" if the value are equals. Thus, the 
  *  test will look like B-delta < A < B+delta
  */	 
 Maths.IsEqual	=	function(A, B, delta)
 {
	 if((A>= B-delta) && ( A <= B+delta) )
	 {
		 return true;
	 }
	 return false;
 }


/*!	@brief The IntersectToFace function will return the intersection point, if any
 *  between a ray and a plan defined by a triangle
 */
Maths.IntersectToFace	=	function(ray, face)
{
	var points = face.ReturnTransformedPosition(matrix);
	
	// First step is to convert the face (wich will always be atriangle) in a plane named P
	// The plane must be exprimed as a Cartesian Equation ax+by+cz+d=0
	// To do so, we only have to get back triangle Normal components
	
	var P = Plane.PlaneFromFace(points);
	
	// Now the plane is initialized, must determine the strait line equation of 
	// the ray. We'll use parametric equation for this.
	// Be A, a point on strait line L, and U, the director vector of L, we have
	// the given parametric equation
	
	//	x 	= 	A.x + 	k*U.x
	//	y	=	A.y	+	k*U.y
	//	z	=	A.z	+	k*U.z
	
	var A = ray.m_StartPosition;
	var U = ray.m_Direction;
	
	// To determine the intersection point between the line L and the plane P,
	// we must fill the P equation with L ones. It'll then give us the "k" value
	// we need to determine the intersection point M
	
	var numerateur 		= - (P.a * A.x)- (P.b * A.y)- (P.c * A.z) - P.d ;
	var denominateur	=  P.a*U.x + P.b*U.y + P.c*U.z;
	
	var k = numerateur/denominateur;
	
	//Now, we just need to use K to get our intersect point from L parametric equation
	
	var intersectionPoint = new Vec4(
										A.x + k*U.x,
										A.y + k*U.y,
										A.z + k*U.z,
										1.0
									);
									
	// Now we get our intersection point M between P and L, we need to know if 
	// M is on our ray
	
	if (Maths.IsBetween(k, 0, ray.m_Length))
	{
		// We also need to know if M is inside our face
	//	Maths.IsPointInPolygon(
		
	}
	return intersectionPoint;
	
} 
